package com.cyou.qb.admin.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cyou.nad.datastore.query.JdbcPage;
import com.cyou.nad.tag.table.TransCol;
import com.cyou.nad.utils.CollectionsUtils;
import com.cyou.nad.utils.MD5;
import com.cyou.qb.common.domain.QiangbaAdmin;
import com.cyou.qb.common.service.AccountService;
import com.cyou.qb.constants.CodeTransConstants;
import com.cyou.qb.constants.Constants;

/**
 * <p>AccountController</p>
 * <p>
 * Description:处理帐号管理相关信息。查询帐号、添加帐号、锁定帐号、删除帐号、修改帐号、密码重置统一返回accountList.ftl
 * </p>
 *
 * @author lidongwei
 * @version 
 * @since 2012-9-19
 */
@Controller
@RequestMapping(value = "/account")
public class AccountController extends BaseController {
	@Autowired
	private AccountService accountService;

	/**
	 * 管理员list
	 */
	@RequestMapping(value = "/index")
	public String index(Model model, HttpServletRequest request, HttpServletResponse response) {
		//获取前台约束
		String loginName = StringUtils.trimToEmpty(request.getParameter("loginName"));
		String dept = StringUtils.trimToEmpty(request.getParameter("dept"));
		String userName = StringUtils.trimToEmpty(request.getParameter("userName"));
		String type = StringUtils.trimToEmpty(request.getParameter("type"));
		String status = StringUtils.trimToEmpty(request.getParameter("status"));
		//获取数据库中数据
		JdbcPage page = accountService.pageQueryAccount(loginName, dept, userName, type, status, getPageNo(),getPageSize(DEFAULT_PAGE_SIZE));
		model.addAttribute("page", page);
		//填充获取数据中需要对应为其他文字的部分
		List<TransCol> trans=new ArrayList<TransCol>(2);
		trans.add(new TransCol("type",CodeTransConstants.ACCOUNT_TYPE));
		trans.add(new TransCol("valid",CodeTransConstants.ACCOUNT_VALID));
		model.addAttribute("trans", trans); 
		saveAllParameters(request);
		//返回页面
		return "account/accountlist";
	}

	/**
	 * 添加管理员，ajax方法
	 */
	@RequestMapping(value = "/saveAdmin")
	public String saveAdmin(Model model, HttpServletRequest request, HttpServletResponse response) throws IOException {
		try {
			//获取添加管理员信息
			String loginName = request.getParameter("accountname1");
			String dept = request.getParameter("depart1");
			String userName = request.getParameter("username1");
			String adminType = request.getParameter("adminType1");
			//断言进行错误处理
			Assert.hasText(loginName, "登录名称不能为空");
			Assert.hasText(dept, "部门不能为空");
			Assert.hasText(userName, "用户名不能为空");
			Assert.hasText(adminType, "类型不能为空");
			//保存到数据库
			accountService.saveOneAdmin(loginName, dept, userName, adminType);
			print(response, "1");
		} catch (Exception e) {
			e.printStackTrace();
			log.error("添加管理员出错", e);
			print(response, "error:" + e.getMessage());
		}
		return null;
	}

	/**
	 * 校验名称是否重复，ajax方法
	 */
	@RequestMapping(value = "/checkAccountLoginName")
	public String checkAccountLoginName(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String loginName = request.getParameter("accountname");
		if (StringUtils.isBlank(loginName)) {
			print(response, "error:用户名称不能为空");
			return null;
		}
		if (accountService.checkLoginNameExist(loginName)) {
			print(response, "error:用户名称" + loginName + "已经存在");
			return null;
		}
		print(response, "1");
		return null;
	}

	/**
	 * 修改管理员基本信息，ajax方法
	 */
	@RequestMapping(value = "/updateAdmin")
	public String updateAdmin(Model model, HttpServletRequest request, HttpServletResponse response) throws IOException {
		try {
			//获取修改后信息
			String aid = StringUtils.trimToEmpty(request.getParameter("aid"));
			String dept = StringUtils.trimToEmpty(request.getParameter("dept"));
			String userName = StringUtils.trimToEmpty(request.getParameter("username"));
			String adminType = StringUtils.trimToEmpty(request.getParameter("adminType"));
			//断言处理错误
			Assert.hasText("aid");
			Assert.hasText(dept, "部门不能为空");
			Assert.hasText(userName, "用户名不能为空");
			Assert.hasText(adminType, "类型不能为空");
			Assert.isTrue(NumberUtils.isDigits(aid), "用户id必须是数字");
			//写入数据库
			accountService.updateAccount(Integer.parseInt(aid), dept, userName, adminType);
			print(response, "1");
		} catch (Exception e) {
			log.error("修改管理员出错", e);
			print(response, "error:" + e.getMessage());
		}
		return null;
	}

	
	/**
	 * 锁定和解锁
	 * Description
	 * @author lidongwei
	 * @since 2012-10-6
	 */
	@RequestMapping(value = "/updateLock")
	public String updateLock(Model model, HttpServletRequest request,HttpServletResponse response) throws IOException {
		try {
			//获取锁定的多个id
			String[] adminIds = request.getParameter("chks").split(",");
			List<String> adminIdList = CollectionsUtils.array2List(adminIds);
			String pTag = request.getParameter("pTag");
			//断言进行错误处理
			Assert.notNull(adminIdList);
			for (int i = 0; i < adminIdList.size(); i++) {
				Assert.hasText(adminIdList.get(i), "adminid不能为空");
				Assert.isTrue(NumberUtils.isDigits(adminIdList.get(i)), "用户id必须是数字");
			}
			//写入数据库
			accountService.updateLockStatus(adminIdList, pTag);
			print(response, "1");
			return null;
		} catch (IOException e) {
			log.error("修改lock出错", e);
			print(response, "error:" + e.getMessage());
			return null;
		}
	}
	/**
	 * 
	 * Description 重置密码
	 * @author lidongwei
	 * @since 2012-10-6
	 */
	@RequestMapping(value = "/restorePassword")
	public String restorePassword(HttpServletRequest request, HttpServletResponse response) throws IOException {
		try {
			String adminIds = request.getParameter("adminId");
			Assert.hasText(adminIds);
			Assert.isTrue(NumberUtils.isDigits(adminIds));
			List<String> adminIdList = new ArrayList<String>();
			adminIdList.add(adminIds);
			accountService.updateResetPassword(adminIdList);
			print(response, "1");
			return null;
		} catch (IOException e) {
			log.error("重置密码出错", e);
			print(response, "error:" + e.getMessage());
			return null;
		}

	}
	/**
	 * 
	 * Description 删除账号
	 * @author lidongwei
	 * @since 2012-10-6
	 */
	@RequestMapping(value = "/removeAdmins")
	public String removeAdmin(Model model, HttpServletRequest request, HttpServletResponse response) throws IOException {
		try {
			String[] adminIds = request.getParameter("chks").split(",");
			List<String> adminIdList = CollectionsUtils.array2List(adminIds);
			Assert.notNull(adminIdList);
			for (int i = 0; i < adminIdList.size(); i++) {
				Assert.hasText(adminIdList.get(i), "adminid不能为空");
				Assert.isTrue(NumberUtils.isDigits(adminIdList.get(i)), "用户id必须是数字");
			}
			accountService.removeAdmin(adminIdList);
			print(response, "1");
			return null;
		} catch (Exception e) {
			log.error("删除账号出错", e);
			print(response, "error:" + e.getMessage());
			return null;
		}

	}

}
